package database

import (
	"log"
	"time"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

var db *gorm.DB

func init() {
	var err error
	dsn := "host=localhost user=zeng password=zenghaifeng dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"
	db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})

	if err != nil {
		log.Fatal("fail to connect database")
	}

	db.AutoMigrate()
	dbs, err := db.DB()
	if err != nil {
		log.Fatal("fail to connect database")
	}
	dbs.SetMaxIdleConns(100)
	dbs.SetMaxOpenConns(100)
	dbs.SetConnMaxLifetime(time.Hour)
}
